<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%
	Integer uid = token.getUid();
	String steps =  Tools.decryptStr(input.getDesc("steps"));
	List<Map> stepList = JSONUtil.fromJson(steps, new TypeReference<List<Map>>(){});
	if (isEmpty(stepList)) {
		Tools.encryptPrint(out,new Message(30041, "请传递正确的steps参数,具体参考文档"));
		return;
	}
	List<Map> existedSteps = QBuilder.db("basedb").select("new map(step_time as date, step as step)").from("steps").where(new Where("uid=", uid).and("hospital=", hospital).and("status=", STATUS_ENABLE)).query().getResultObj();	
	Map<String, Integer> stepTime = new HashMap<String, Integer>();
	for (Map map:existedSteps) {
		stepTime.put(Tools.getDesc(map.get("date"), ""), Tools.getInt(map.get("step") + "", 0));
	}
	Integer addCount = 0;
	Integer updateCount = 0;
	List<Map> addItems = new ArrayList<Map>();
	for(Map stepMap:stepList) {
		String date = Tools.getDesc(stepMap.get("date"), "");
		Integer step = Tools.getInt(stepMap.get("step") + "", 0);
		Integer todayStep = stepTime.get(date);
		if(isEmpty(todayStep)) {
			Map map = new HashMap();
			map.put("step_time", date);
			map.put("step", step);
			map.put("uid", uid );
			map.put("status", STATUS_ENABLE);
			map.put("hospital", hospital);
			map.put("push", 0);
			map.put("create_time", System.currentTimeMillis());
			map.put("json", "{}");
			addItems.add(map);
			addCount++;
		} else {
			// 如果是当天的步数，且后传递来的值大于之前的值，则更新
			if((DateUtil.formatDate(new Date()).equals(date)) && (step > todayStep)) {
				QBuilder.db("basedb").execHQL("update steps set step= " + step + " where uid=" + uid + " and step_time='" + date + "' and status = " + STATUS_ENABLE + " and hospital = " + hospital );
				updateCount++;
			}
		}
	}
	DBTools.db("basedb").addItem("steps", addItems);	
	Map resultMap = new HashMap();
	resultMap.put("desc", String.format("本次增加了%s条数据, 更新了%s条数据", addCount, updateCount));
	resultMap.put("update",  updateCount);
	resultMap.put("add",  addCount);
	msg.put("data", resultMap);
	Tools.encryptPrint(out, msg.toJson());
%>